package com.example.myapplication

import com.abc.robot01.retrofit.UploadFileService
import com.google.gson.GsonBuilder
import kotlinx.coroutines.test.runTest
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.io.IOException

class UploadFileServiceTest {
    @Test
    fun addition_isCorrect() {
        assertEquals(4, 2 + 2)
    }

    private  lateinit var  mockWebServer: MockWebServer ;

    private lateinit var uploadFileService: UploadFileService

    @Before
    @Throws(IOException::class)
    fun setup() {
        // Create a MockWebServer instance
        mockWebServer = MockWebServer()
        mockWebServer.start()
        // Create Gson instance
        val gson = GsonBuilder()
            .setLenient()
            .create()
        // Create Retrofit instance with MockWebServer URL and Gson converter
        val retrofit = Retrofit.Builder()
            .baseUrl(mockWebServer.url("/")) // Use mock web server URL
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build()
        // Create an instance of your Retrofit service interface
        uploadFileService = retrofit.create(UploadFileService::class.java)
    }

    @After
    @Throws(IOException::class)
    fun tearDown() {
        // Shutdown the MockWebServer
        mockWebServer.shutdown()
    }

    @Test
    fun testA01_success() = runTest {
        val mockResponse = "123"
        mockWebServer.enqueue(
            MockResponse()
                .setResponseCode(200)
                .setBody(mockResponse))
        val  result  = uploadFileService.a01("haha");
        assertEquals(mockResponse, result)
    }





}